package FIT::Document::Handler::Expression;

use FIT::Class;
use FIT::Util::SpecialChars;
use FIT::Document::Transformer;

extends 'FIT::Document::Handler::Base';

sub handle {
    my ($fixture, $cell, @handlers) = @_;
    $self->eval_expression($cell)
        if $cell->detect_mark($MARKER_EXPRESSION);
    $self->run_next_handler($fixture, $cell, @handlers);
}

sub eval_expression {
    my $cell = shift;
    $cell->clear_mark($MARKER_EXPRESSION);
    my $code = $cell->trimmed_text;
    my $result = eval $code;
    my $error = $@;

    if ($error) {
        $cell->insert_mark($MARKER_EXPRESSION);
        die "Error executing expression [$code]- $error";
    }

    # TODO: escape
    # TODO: should use paint of type adapter
    $cell->replace_text($result);
}


=head1 NAME

FIT::Document::Handler::Expression - write code in cells


=head1 DESCRIPTION

This handler lets you write code in cells. The code will be executed, and the
result inserted into the cell instead of the expression.


=cut

